package controller;

import dao.DaoException;
import dao.productDao.ProductDaoImpl;
import model.GoodsItem;
import model.Product;
import model.ShoppingCart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import static utilities.MyConst.CART;

@WebServlet(name = "ControllerCartServlet", urlPatterns = {"/addToCart", "/deleteItem", "/deleteAllCart"})
public class ControllerCartServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ProductDaoImpl dao = new ProductDaoImpl();
        int productId = 0;
        int quantity = 0;

        try {
            productId = Integer.parseInt(request.getParameter("id"));
            quantity = Integer.parseInt(request.getParameter("quantity"));
        } catch (NumberFormatException e) {
            System.out.println(e.toString());
        }

        Product product;
        try {
            product = dao.findById(productId);
            if (product != null && quantity >= 0 && product.getStock() - quantity > 0) {
                GoodsItem goodsItem = new GoodsItem(product, quantity);
                HttpSession session = request.getSession();

                ShoppingCart cart = (ShoppingCart) session.getAttribute(CART);
                if (cart == null) {
                    cart = new ShoppingCart();
                    session.setAttribute(CART, cart);
                }
                try {
                    dao.updateStockById(productId, quantity);
                } catch (DaoException e) {
                    e.printStackTrace();
                }
                cart.add(goodsItem);
                response.sendRedirect("showCart.jsp");
            } else {
                request.setAttribute("msg", "库存不足");
                request.getRequestDispatcher("/showProduct.jsp").forward(request, response);
            }
        } catch (DaoException e) {
            e.printStackTrace();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String uri = request.getRequestURI();

        if (uri.endsWith("deleteItem")) {
            deleteItem(request, response);
        } else if (uri.endsWith("deleteAllCart")) {
            deleteAllCart(request, response);
        }
    }

    private void deleteAllCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        ShoppingCart cart = (ShoppingCart) session.getAttribute(CART);
        cart.clear();
        response.sendRedirect("showCart.jsp");
    }

    private void deleteItem(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();

        ShoppingCart cart = (ShoppingCart) session.getAttribute(CART);
        try {
            int id = Integer.parseInt(request.getParameter("id"));
            GoodsItem item = null;
            for (GoodsItem shopItem : cart.getItems()) {
                if (shopItem.getProduct().getPid() == id) {
                    item = shopItem;
                    break;
                }
            }
            if (item != null) {
                cart.remove(item.getProduct().getPid());
            }
        } catch (NumberFormatException e) {
            System.out.println("发生异常:" + e.getMessage());
        }

        session.setAttribute(CART, cart);
        response.sendRedirect("showCart.jsp");
    }
}
